package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollUtil;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.mongo.CommentService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

@DubboService
public class CommentServiceImpl implements CommentService {

    @Autowired
    MongoTemplate mongoTemplate;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public Integer saveComment(Comment comment) {
        //1.调用mongoTemplate保存comment
        mongoTemplate.save(comment);

        //2.修改该动态指定类型的数量
        //2.1 根据操作类型,查询出动态详情
        switch (comment.getCommentType()){
            //点赞
            case 1:{
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                //2.2 修改操作的数量 ++
                movement.setLikeCount(movement.getLikeCount()+1);
                //2.3 保存最新的动态
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getLikeCount();
            }
            //评论
            case 2: {
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                //2.2 修改操作的数量 ++
                movement.setCommentCount(movement.getCommentCount() + 1);
                //2.3 保存最新的动态
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getCommentCount();
            }
            //喜欢
            case 3: {
                Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
                //2.2 修改操作的数量 ++
                movement.setLoveCount(movement.getLoveCount() + 1);
                //2.3 保存最新的动态
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getLoveCount();
            }
            case 4:
            case 5:
            case 6:
            default:
                return 0;
        }
    }

    @Override
    public Integer removeComment(Long userId, ObjectId publishId, Integer commentType) {
        //1.创建条件对象
        Query query = new Query(
                Criteria.where("userId").is(userId)
                    .and("publishId").is(publishId)
                    .and("commentType").is(commentType)
        );
        //2.从mongo中移除改记录
        mongoTemplate.remove(query,Comment.class);

        //2.1 根据操作类型,查询出动态详情
        switch (commentType){
            //点赞
            case 1:{
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //2.2 修改操作的数量 --
                movement.setLikeCount(movement.getLikeCount() - 1);
                //2.3 保存最新的动态
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getLikeCount();
            }
            //评论
            case 2: {
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //2.2 修改操作的数量 --
                movement.setCommentCount(movement.getCommentCount() - 1);
                //2.3 保存最新的动态
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getCommentCount();
            }
            //喜欢
            case 3: {
                Movement movement = mongoTemplate.findById(publishId, Movement.class);
                //2.2 修改操作的数量 --
                movement.setLoveCount(movement.getLoveCount() - 1);
                //2.3 保存最新的动态
                mongoTemplate.save(movement);
                //2.4 返回最新的数量
                return movement.getLoveCount();
            }
            case 4:
            case 5:
            case 6:
            default:
                return 0;
        }
    }

    @Override
    public PageBeanVo findCommentByPage(ObjectId publishId, Integer pageNum, Integer pageSize) {
        //1.创建条件对象(动态id,comment类型,分页,时间排序)
        Query query = new Query(
                Criteria.where("publishId").is(publishId)
                .and("commentType").is(2)
        ).with(Sort.by(Sort.Order.desc("created")))
         .skip((pageNum-1)*pageSize).limit(pageSize);

        //2.查询comment表,返回list
        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        //3.封装PageBeanVo且返回
        return new PageBeanVo(pageNum,pageSize,Long.valueOf(comments.size()),comments);
    }

    @Override
    public PageBeanVo findByPage(Long userId, Integer pageNum, Integer pageSize, Integer commentType) {
        //1.创建条件对象(登陆者id,分页,排序,类型)
        Query query = new Query(Criteria.where("publishUserId").is(userId).and("commentType").is(commentType))
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((pageNum-1)*pageSize).limit(pageSize);

        //2.查询comment表返回一个list
        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        //3.封装pageBeanVo,返回即可
        Long counts = CollUtil.isEmpty(comments)?0L:comments.size();
        return new PageBeanVo(pageNum,pageSize,counts,comments);
    }
}
